package Http;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CookServlet
 */
@WebServlet("/CookServlet")
public class CookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		ArrayList as=new ArrayList();
		as.add("123");
		as.add("12");
//		
//		String name=(String) request.getAttribute("username");
//		String pass=(String) request.getAttribute("password");
		PrintWriter pw = response.getWriter();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String xuan = request.getParameter("chex");
		if (username.equals("123") && password.equals("123")) {
			if (xuan!=null ) {
				Cookie cookie = new Cookie("username", username);
				Cookie cookie1 = new Cookie("password", password);
				response.addCookie(cookie);
				response.addCookie(cookie1);
				cookie.setMaxAge(60*60*24*7);
				cookie1.setMaxAge(60*60*24*7);
				pw.write(username + " " + "欢迎登陆");
			} else {
				pw.write(username + " " + "欢迎登陆");
			}

		} else {
			response.sendRedirect("Login.html");
		}
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
